<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>source manual page - Built-In Commands</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl Commands</a> <small>&gt;</small> source</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<H3><A NAME="M2">NAME</A></H3>
source &mdash; Evaluate a file or resource as a Tcl script
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>source </B><I>fileName</I><BR>
<B>source</B> <B>-encoding </B><I>encodingName fileName</I><BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
This command takes the contents of the specified file or resource
and passes it to the Tcl interpreter as a text script.  The return
value from <B>source</B> is the return value of the last command
executed in the script.  If an error occurs in evaluating the contents
of the script then the <B>source</B> command will return that error.
If a <B><A HREF="../TclCmd/return.htm">return</A></B> command is invoked from within the script then the
remainder of the file will be skipped and the <B>source</B> command
will return normally with the result from the <B><A HREF="../TclCmd/return.htm">return</A></B> command.
<P>
The end-of-file character for files is
&ldquo;&#92;32&rdquo;
(^Z) for all platforms.
The source command will read files up to this character.  This
restriction does not exist for the <B><A HREF="../TclCmd/read.htm">read</A></B> or <B><A HREF="../TclCmd/gets.htm">gets</A></B> commands,
allowing for files containing code and data segments (scripted documents).
If you require a
&ldquo;^Z&rdquo;
in code for string comparison, you can use
&ldquo;&#92;032&rdquo;
or
&ldquo;&#92;u001a&rdquo;,
which will be safely substituted by the Tcl interpreter into
&ldquo;^Z&rdquo;.
<P>
A leading BOM (Byte order mark) contained in the file is ignored for unicode encodings (utf-8, unicode).
<P>
The <B>-encoding</B> option is used to specify the encoding of
the data stored in <I>fileName</I>.  When the <B>-encoding</B> option
is omitted, the system encoding is assumed.
<H3><A NAME="M5">EXAMPLE</A></H3>
Run the script in the file <B>foo.tcl</B> and then the script in the
file <B>bar.tcl</B>:
<P>
<PRE><B>source</B> foo.tcl
<B>source</B> bar.tcl</PRE>
<P>
Alternatively:
<P>
<PRE>foreach scriptFile {foo.tcl bar.tcl} {
    <B>source</B> $scriptFile
}</PRE>
<H3><A NAME="M6">SEE ALSO</A></H3>
<B><A HREF="../TclCmd/file.htm">file</A></B>, <B><A HREF="../TclCmd/cd.htm">cd</A></B>, <B><A HREF="../TclCmd/encoding.htm">encoding</A></B>, <B><A HREF="../TclCmd/info.htm">info</A></B>
<H3><A NAME="M7">KEYWORDS</A></H3>
<A href="../Keywords/F.htm#file">file</A>, <A href="../Keywords/S.htm#script">script</A>
<div class="copy">Copyright &copy; 1993 The Regents of the University of California.
<BR>Copyright &copy; 1994-1996 Sun Microsystems, Inc.
<BR>Copyright &copy; 2000 Scriptics Corporation.
</div>
</BODY></HTML>
